package ext.tianma.bom.service.sourcingInterface;

import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import ext.tianma.bom.service.TMECOUtil;
import wt.log4j.LogR;

/**
 * sourcing相关controller
 * 
 * @author lilin
 * @date 2019-01-21 16:58:55 +0800
 */
@Controller
@RequestMapping({ "/SourcingInterfaceController" })
public class SourcingInterfaceController {
	
	private static final Logger log = LogR.getLogger(SourcingInterfaceController.class.getName());
	
	
	/**
	 * 根据sourcing传回的参数判断plm中的业务
	 * 
	 * @param type
	 *            驳回或者发布
	 * @param rejectReason
	 *            驳回原因
	 * @param supplierInfo
	 *            供应商信息
	 * @param partNumber
	 *            物料编号
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/modifyAttrOrSendEmail", method = { RequestMethod.POST, RequestMethod.GET })
	public void modifyAttrOrSendEmail(@RequestParam(value = "type", required = true) String type,
			@RequestParam(value = "rejectReason", required = false) String rejectReason,
			@RequestParam(value = "supplierInfo", required = false) String supplierInfo,
			@RequestParam(value = "buyer", required = false) String buyer,
			@RequestParam(value = "partNumber", required = false) String partNumber, HttpServletResponse response)
			throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");// 返回文本值
		try {
			String result = SourcingInterfaceService.modifyAttrOrSendEmail(type, rejectReason, supplierInfo, buyer,
					partNumber);// 根据type来判断业务
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	/**
	 * Soucring 接口  根据type来判断业务
	 * @param data
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/materialSynchronous", method = { RequestMethod.POST, RequestMethod.GET })
	public void materialSynchronous(@RequestBody String data, HttpServletResponse response)
			throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");// 返回文本值
		try {
			String result = "";
			// 逻辑处理
			JSONObject obj = new JSONObject(data);
			System.out.println("传递过来的数据======="+obj.toString());
			
			if (obj.has("datalist")) {
				System.out.println("====里面有数据,datalist======"+obj.toString());
				JSONArray array = new JSONArray(obj.getString("datalist"));
				//result = BOMHelp.updatePartAmountValidate(array);
				
				result =  SourcingInterfaceService.handleSoucreInterface(array);
				log.info("===============amount result:" + result);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * Soucring 接口  根据type来判断业务
	 * 用于二级BOM Sourcing退回处理二级物料信息
	 * @param data
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/materialModification", method = { RequestMethod.POST, RequestMethod.GET })
	public void materialModification(@RequestBody String data, HttpServletResponse response)
			throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");// 返回文本值
		try {
			String result = "";
			// 逻辑处理
			JSONObject obj = new JSONObject(data);
			System.out.println("传递过来的数据======="+obj.toString());
			
			if (obj.has("datalist")) {
				System.out.println("====里面有数据,datalist======"+obj.toString());
				JSONArray array = new JSONArray(obj.getString("datalist"));
				//result = BOMHelp.updatePartAmountValidate(array);
				
				result =  SourcingInterfaceService.materialModificationSoucreInterface(array);
				log.info("===============amount result:" + result);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}

}
